package p200;

public class Leetcode_234 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public boolean isPalindrome(ListNode head) {
        ListNode slow = head, fast = head, prev = null;
        // 快慢指针找到中点
        while (fast != null) {
            slow = slow.next;
            fast = fast.next != null ? fast.next.next : fast.next;
        }
        // 逆序后半部分
        while (slow != null) {
            ListNode r = slow.next;
            slow.next = prev;
            prev = slow;
            slow = r;
        }
        // 检查是否回文
        while (head != null && prev != null) {
            if (head.val != prev.val) {
                return false;
            }
            head = head.next;
            prev = prev.next;
        }
        return true;
    }

    public static void main(String[] args) {

    }
}
